package com.x.ant.manage;

import com.x.ant.dao.AreaDao;
import com.x.ant.dao.BillDao;
import com.x.ant.dao.TaskDao;
import com.x.ant.model.*;
import com.x.framework.manage.BaseManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BillManage extends BaseManage {
    @Autowired
    private BillDao billDao;

    @Autowired
    private TaskDao taskDao;

    public void updateBill(Bill bill) throws Exception {
        if(bill.getBillState() == 1) {
            BillTruck billTruck = new BillTruck();
            billTruck.setBillId(bill.getBillId());
            List<BillTruck> billTruckList = billDao.selectBillTruckList(billTruck);
            int num = billTruckList.size();
            if (num > 0) {
                Task task = new Task();
                task.setTaskId(bill.getTaskId());
                List<Task> taskList = taskDao.selectTaskList(task);
                task = taskList.get(0);
                int taskCostPrice = task.getTaskCostPrice();
                Money money = new Money();
                money.setDeptId(task.getDeptId());
                money.setMoneyCreateTime(this.getDate(0));
                money.setTaskId(task.getTaskId());
                money.setMoneyAmount(taskCostPrice);
                money.setMoneyInOut(1);
                money.setMoneyState(1);
                money.setMoneyType(1);
                for (BillTruck bt : billTruckList) {
                    money.setMoneyRemark(bill.getBillCode() + bt.getTruck().getTruckPlateHeader() + bt.getTruck().getTruckPlateMiddle() + bt.getTruck().getTruckPlateCode() + "收费");
                    this.insert(money);
                    money.setMoneyId(null);
                }
            }
        }
        this.update(bill);
    }

    public BillTruck insertBillTruck(BillTruck billTruck, String truckIds) throws Exception {
        String billId = billTruck.getBillId();
        if (!this.isString(billId)) {
            Bill bill = billTruck.getBill();
            bill.setBillCode(bill.getBillCode() + this.getUid(12) + "-" + this.getUid(3));
            bill.setBillCreateTime(this.getDate(0));
            bill = this.insert(bill);
            billId = bill.getBillId();
        }
        billTruck.setBillId(billId);
        billTruck.setBillTruckCreateTime(this.getDate(0));
        if (this.isString(truckIds)) {
            String[] truckIdArray = truckIds.split(",");
            for (String truckId : truckIdArray) {
                billTruck.setTruckId(truckId);
                this.insert(billTruck);
            }

            String taskId = billTruck.getBill().getTaskId();
            Task task = new Task();
            task.setTaskId(taskId);
            task.setTaskTruckOccupyNum(truckIdArray.length);
            taskDao.updateTaskTruckNum(task);
        }
        return billTruck;
    }

    public void updateBillTruck(BillTruck billTruck) throws Exception {
        int billTruckState = billTruck.getBillTruckState();
        if(billTruckState == 0) {
            Bill bill = billTruck.getBill();
            Truck truck = billTruck.getTruck();
            Task task = bill.getTask();
            task.setTaskTruckOccupyNum(-1);
            taskDao.updateTaskTruckNum(task);

            Money money = new Money();
            money.setDeptId(task.getDeptId());
            money.setMoneyCreateTime(this.getDate(0));
            money.setTaskId(task.getTaskId());
            money.setMoneyAmount(task.getTaskCostPrice());
            money.setMoneyInOut(-1);
            money.setMoneyRemark(bill.getBillCode() + " " + truck.getTruckPlateHeader() + truck.getTruckPlateMiddle() + truck.getTruckPlateCode() + "退款");
            money.setMoneyState(1);
            money.setMoneyType(1);
            this.insert(money);
        }
        this.update(billTruck);
    }

    public void deleteBillTruck(BillTruck billTruck) throws Exception {
        Bill bill = billTruck.getBill();
        Task task = new Task();
        task.setTaskId(bill.getTaskId());
        task.setTaskTruckOccupyNum(-1);
        taskDao.updateTaskTruckNum(task);
        this.delete(billTruck);
    }
}